﻿using LaoLuo;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LaoLuo.Common.Test
{


    /// <summary>
    ///这是 SizedListTest 的测试类，旨在
    ///包含所有 SizedListTest 单元测试
    ///</summary>
    [TestClass()]
    public class SizedCollectionTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private void SizedCollectionTestAll(ICollection<int> l1, ICollection<Class1> l2)
        {
            foreach (var item in intArray)
            {
                l1.Add(item);
            }

            Assert.AreEqual(size, l1.Count);
            Assert.AreEqual(intArray.Reverse().Take(size).GetJoint(), l1.GetJoint());
            var toRemove = l1.Skip(1).First();
            Assert.IsTrue(l1.Remove(toRemove));
            Assert.AreEqual(size - 1, l1.Count);
            Assert.AreEqual(intArray.Reverse().Take(size).Except(new List<int> { toRemove }).GetJoint(), l1.GetJoint());

            for (int i = 0; i < 10; i++)
            {
                l2.Add(new Class1(i));
            }

            Assert.AreEqual(size, l2.Count);
            Assert.AreEqual("9,8,7", string.Join(",", l2));

            var toRemove2 = new Class1(100);
            l2.Add(toRemove2);

            Assert.AreEqual("100,9,8", string.Join(",", l2));
            l2.Remove(toRemove2);
            Assert.AreEqual("9,8", string.Join(",", l2));
        }

        static int size = 3;
        static int[] intArray = { 2, 3, 532, 1, 3, 0, 32, 42, 34, 123 };

        [TestMethod()]
        public void Test()
        {
            SizedCollectionTestAll(new SizedLinkedList<int>(size), new SizedLinkedList<Class1>(size));
            SizedCollectionTestAll(new SizedList<int>(size), new SizedList<Class1>(size));
        }

        class Class1 : IComparable<Class1>
        {
            public Class1(int value)
            {
                Value = value;
            }

            public int Value { get; private set; }

            public override string ToString()
            {
                return Value.ToString();
            }

            public int CompareTo(Class1 other)
            {
                return Value.CompareTo(other.Value);
            }
        }
    }
}
